package com.student.controller;

import com.student.bean.Question;
import com.student.bean.Result;
import com.student.bean.User;
import com.student.service.TestService;
import com.student.utils.AjaxResult;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class TestController {
    @Resource
    private TestService testService;

    @ResponseBody
    @RequestMapping(value = "/allQuestion")
    public AjaxResult allTest() {
        List<Question> questionList = testService.allQuestion();
        return AjaxResult.getOk(questionList);
    }

    @RequestMapping(value = "/addQuestion")
    public void addQuestion(@RequestParam(value = "content") String content) {
        Question question = new Question();
        question.setQuestion(content);
        testService.addQuestion(question);
    }

    @RequestMapping(value = "/test")
    public void test(@RequestParam(value = "q1") String q1,
                     @RequestParam(value = "q2") String q2,
                     @RequestParam(value = "q3") String q3,
                     @RequestParam(value = "q4") String q4,
                     @RequestParam(value = "q5") String q5,
                     @RequestParam(value = "q6") String q6,
                     @RequestParam(value = "q7") String q7,
                     @RequestParam(value = "q8") String q8,
                     @RequestParam(value = "q9") String q9,
                     @RequestParam(value = "q10") String q10,
                     @RequestParam(value = "q11") String q11,
                     @RequestParam(value = "q12") String q12,
                     @RequestParam(value = "q13") String q13,
                     @RequestParam(value = "q14") String q14,
                     @RequestParam(value = "q15") String q15,
                     @RequestParam(value = "q16") String q16,
                     @RequestParam(value = "q17") String q17,
                     @RequestParam(value = "q18") String q18,
                     @RequestParam(value = "q19") String q19,
                     @RequestParam(value = "q20") String q20,
                     @RequestParam(value = "date") String dateString,
                     HttpSession session) throws ParseException {
        Result result = new Result();
        result.setQ1(q1);
        result.setQ2(q2);
        result.setQ3(q3);
        result.setQ4(q4);
        result.setQ5(q5);
        result.setQ6(q6);
        result.setQ7(q7);
        result.setQ8(q8);
        result.setQ9(q9);
        result.setQ10(q10);
        result.setQ11(q11);
        result.setQ12(q12);
        result.setQ13(q13);
        result.setQ14(q14);
        result.setQ15(q15);
        result.setQ16(q16);
        result.setQ17(q17);
        result.setQ18(q18);
        result.setQ19(q19);
        result.setQ20(q20);

        List<String> resultList = new ArrayList<>();
        resultList.add(q1);
        resultList.add(q2);
        resultList.add(q3);
        resultList.add(q4);
        resultList.add(q5);
        resultList.add(q6);
        resultList.add(q7);
        resultList.add(q8);
        resultList.add(q9);
        resultList.add(q10);
        resultList.add(q11);
        resultList.add(q12);
        resultList.add(q13);
        resultList.add(q14);
        resultList.add(q15);
        resultList.add(q16);
        resultList.add(q17);
        resultList.add(q18);
        resultList.add(q19);
        resultList.add(q20);

        int a = 0;
        int b = 0;
        int c = 0;
        int d = 0;

        for (String s : resultList) {
            switch (s) {
                case "A":
                    a++;
                    break;
                case "B":
                    b++;
                    break;
                case "C":
                    c++;
                    break;
                case "D":
                    d++;
                    break;
            }
        }
        result.setScore(b + 2 * c + 3 * d);

        System.out.println(dateString);
        //将获取到的时间字符串转换为Date格式数据
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = simpleDateFormat.parse(dateString);
        result.setDate(date);
        //获取登录用户用户信息
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        Integer userid = user.getUserid();
        result.setUserid(userid);
        //将结果添加到数据库
        testService.addResult(result);
        session.setAttribute("result", result);
    }

    @ResponseBody
    @RequestMapping(value = "/moodindex")
    public AjaxResult moodindex(HttpSession session) {
        Result result = (Result) session.getAttribute("result");
        System.out.println(result.getScore());
        return AjaxResult.getOk(result);
    }

    @ResponseBody
    @RequestMapping(value = "/allResult")
    public AjaxResult allResult(){
        List<Result> resultList = testService.allResult();
        return AjaxResult.getOk(resultList);
    }
}
